/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.engin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

/**
 *
 * @author escapes
 */
public class DatabaseExpoert {

    public static Charset charset = Charset.forName("UTF-8");
    public static CharsetEncoder encoder = charset.newEncoder();
    public static CharsetDecoder decoder = charset.newDecoder();

    public String exportDb(String user, String password, String filePath, String databaseNamespace) {
        String output = "";
        FileWriter fw = null;
        String dumpCommand = "mysqldump -u" + user + " -p" + password + " --default-character-set=utf8 --add-drop-database=true " + databaseNamespace + " -r" + filePath;
        File data = new File(filePath);
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(dumpCommand);
            String out=readFile(filePath);
            writeOutput(filePath.replace(".sql", ".txt"), out);
        } catch (IOException e) {
            e.printStackTrace();
            output = "data failure" + e.getLocalizedMessage();
            System.out.println("failed" + output);
        }

        System.out.println("output=" + output);
        return output;
    }

    public String importDb(String user, String password, String filePath, String databaseNamespace) {
        String output = "";

        String[] executeCmd = new String[]{"mysql", databaseNamespace, "-u" + user, "-p" + password, "-e", " source " + filePath};

        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(executeCmd);

        } catch (IOException e) {
            e.printStackTrace();
            output = "data failure" + e.getLocalizedMessage();
            System.out.println("failed" + output);
        }
        System.out.println("output=" + output);
        return output;
    }

    public String readFile(String path) {
       

        StringBuffer buffer = new StringBuffer();
        try {
            FileInputStream fis = new FileInputStream(path);
            InputStreamReader isr = new InputStreamReader(fis, "UTF8");
            Reader in = new BufferedReader(isr);
            int ch;
            while ((ch = in.read()) > -1) {
                buffer.append((char) ch);
            }
            in.close();
            
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

       
    }

    public void writeOutput(String path,String str) {
    try {
        FileOutputStream fos = new FileOutputStream(path);
        Writer out = new OutputStreamWriter(fos, "UTF8");
        out.write(str);
        out.close();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
}
}
